; 较完善的示例

#Include MouseEvent.ahk

g := Gui('+AlwaysOnTop -Caption +ToolWindow +Border')
g.SetFont('s18', 'consolas')
g.MarginX := g.MarginY := 10
sb := g.AddStatusBar()
me := MouseEvent(g.Hwnd)

; #region 标题栏
titleBar := g.AddText('x10 y10 w350 Backgroundc6f0d5 Center Border', 'Drag me')
me.RegisterOnCondition(MouseEvent.Events.MOUSE_MOVE,
  (point, *) => me.LD && inEl(me.LDPoint, titleBar) && inEl(point, titleBar), ; 左键按下 & 左键按下的坐标在标题栏中 & 当前坐标在标题栏中
  MoveWin) ; 不使用 (*) => PostMessage(0xA1, 2)，因为左键弹起事件不会触发
; #endregion

; #region 滚动事件
wheelTest := g.AddText('x10 w390 Center h30 Backgroundc19423', 1)
me.RegisterOnCondition(MouseEvent.Events.MOUSE_WHEEL,
  (point, *) => inEl(point.ConvertToClient(g), wheelTest),
  (_, up, *) => wheelTest.Text += up ? 1 : -1)

me.Register(MouseEvent.Events.MOUSE_WHEEL_UP, (*) => sb.SetText('WhellUp'))
me.Register(MouseEvent.Events.MOUSE_WHEEL_DOWN, (*) => sb.SetText('WhellDown'))
; #endregion

; #region 模拟关闭按钮
closeBtn := g.AddText('w30 x370 y10 Center Backgroundffd0d0 Border', 'X')
InitCloseBnt(closeBtn, me)

InitCloseBnt(btn, e) {
  static hoverC := '00bfff', pressedC := 'ff0000', defaultC := 'ffd0d0'
  ; 点击事件
  e.Register(MouseEvent.Events.LBUTTON_DOWN, _onClick)
  e.RegisterOnCondition(MouseEvent.Events.LBUTTON_UP, (*) => btn['pressed'], _onUp)
  ; 悬浮
  e.RegisterOnCondition(MouseEvent.Events.MOUSE_MOVE, (point, *) => inEl(point, btn), _onEnter, _onLeave)

  _onEnter(*) {
    if btn['hover']
      return
    btn.Opt('Background' hoverC)
    if btn['pressed']
      btn.Opt('Background' pressedC)
    btn.Redraw(), btn.hover := true
  }

  _onLeave(*) {
    if btn['hover'] {
      btn.Opt('Background' defaultC), btn.Redraw(), btn.hover := false
    }
  }

  _onClick(point, *) {
    if inEl(point, btn) {
      btn.Opt('Background' pressedC), btn.Redraw(), btn.pressed := true
    }
  }

  _onUp(point, *) {
    if inEl(point, btn)
      ExitApp()
    btn.pressed := false
  }
}
; #endregion


texts := []
loop 3 {
  i := A_Index - 1
  loop 10
    texts.Push g.AddText(Format('y{} x{} h30 w30 Backgrounda2a2a2', i * 40 + 90, (A_Index - 1) * 40 + 10), A_Index - 1 i)
}

me.Register(MouseEvent.Events.LBUTTON_DOWN, OnLBD) ; 统一处理，防止过多回调
me.Register(MouseEvent.Events.MOUSE_MOVE, OnMV)

OnMV(point, *) {
  sb.SetText('MV' point.x '-' point.y)
  for v in texts {
    if inEl(point, v) { ; 进入元素
      v.Opt('Backgroundff0000')
      if me.LD
        v.Opt('Background005780')
      v.Redraw(), v.hover := true
    } else if v['hover'] ; 离开
      v.Opt('Backgroundb9ff80'), v.Redraw(), v.hover := false
  }
}

OnLBD(point, *) {
  sb.SetText(point.x '-' point.y)
  for v in texts {
    v.getpos(&_x, &_y, &_w, &_h)
    if inBox(point, _x, _y, _w, _h)
      v.Opt('Background005780'), v.Redraw()
  }
}

me.Register(MouseEvent.Events.RBUTTON_UP, OnContext)

OnContext(point, *) {
  m := Menu()
  m.add('test1', Noop)
  m.add('test2', Noop)
  m.add('test3', Noop)
  m.add('test4', Noop)
  m.Show(point.x, point.y)
}
g.Show()


; ========

inBox(pos, x, y, w, h) {
  return pos.x >= x && pos.x <= x + w
    && pos.y >= y && pos.y <= y + h
}
inEl(pos, el) {
  el.GetPos(&x, &y, &w, &h)
  return inBox(pos, x, y, w, h)
}
MoveWin(*) {
  CoordMode "Mouse", "Screen"
  MouseGetPos(&px, &py), g.GetPos(&wx, &wy)
  dx := wx - px, dy := wy - py
  While GetKeyState("LButton", "P")
    MouseGetPos(&nx, &ny), g.Move(nx + dx, ny + dy)
}